package work

import (
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/work"

	"github.com/gin-gonic/gin"
)

func Update(c *gin.Context) {
	parkName := c.PostForm("park_name")
	carNumber := c.PostForm("car_number")
	question := c.PostForm("question")
	id := c.PostForm("id")

	//获取上传文件
	randomFileName, err := controller.GetUploadFile(c, "image_new")
	if err != nil {
		controller.Fail(c, err.Error())
		return
	}

	//管理员id
	adminId, _ := c.Get("admin_id")

	systemSection := config.Cfg.Section("SYSTEM")
	imgUrl := systemSection.Key("IMAGE_URL").String()

	var workOrderStruct work.WorkOrderStruct

	model.UpdateModelData(
		model.Db,
		workOrderStruct.TableName(),
		map[string]interface{}{
			"admin_id": adminId,
			"id":       id,
		},
		map[string]interface{}{
			"car_number": carNumber,
			"park_name":  parkName,
			"question":   question,
			"image":      imgUrl + randomFileName,
		},
	)

	controller.Success(c, "")
}
